#include "downloadmanager.h"
#include "downloaditem.h"
#include <QtCore/QDebug>

DownloadManager::DownloadManager(QObject *parent)
    : QObject(parent)
    , m_networkManager(new QNetworkAccessManager(this))
{
}

DownloadManager::~DownloadManager()
{
    qDeleteAll(m_downloads);
}

void DownloadManager::addDownload(const QString &url)
{
    DownloadItem *item = new DownloadItem(url, this);
    m_downloads.append(item);
}

void DownloadManager::startDownload(int index)
{
    if (index >= 0 && index < m_downloads.size()) {
        m_downloads.at(index)->start();
    }
}

void DownloadManager::pauseDownload(int index)
{
    if (index >= 0 && index < m_downloads.size()) {
        m_downloads.at(index)->pause();
    }
}

void DownloadManager::removeDownload(int index)
{
    if (index >= 0 && index < m_downloads.size()) {
        DownloadItem *item = m_downloads.takeAt(index);
        item->deleteLater();
    }
}

int DownloadManager::downloadCount() const
{
    return m_downloads.size();
}

DownloadItem* DownloadManager::downloadAt(int index) const
{
    if (index >= 0 && index < m_downloads.size()) {
        return m_downloads.at(index);
    }
    return nullptr;
}